import websocket
import json

def parseBuffer(buffer):
    bufferString = buffer.decode("utf-8")
    code = int(bufferString)
    arr = bufferString.find("[")
    obj = bufferString.find("{")
    firstChar = max(arr, obj) if (arr == -1 or obj == -1) else min(arr, obj)
    parsed = json.loads(bufferString[firstChar:])

    return {
        "code": code,
        "event": parsed[0] if len(parsed) > 1 else "unknown",
        "payload": parsed[1] if len(parsed) > 1 else parsed
    }

def handleFortyTwo(event):
    switcher = {
        "unknown": None,
        "0x49cf6f5d44e70224e2e23fdcdd2c053f30ada28b.feeds.activity.eventsCreated": handleEventCreated(event["payload"])
    }
    switcher.get(event["event"], lambda: None)()

def receive_data():
    # -------------------------------------------------- v3 url wss://feeds.prod.blur.io/socket.io/?tabId=pKzRU7SRBQDn&storageId=n3kEOR5uxjEF&EIO=4&transport=websocket -------------------------------------------------------#
    # url = "wss://feeds.prod.blur.io/socket.io/?tabId=pKzRU7SRBQDn&storageId=n3kEOR5uxjEF&EIO=4&transport=websocket"
    #  -------------------------------------------------- v4 url wss://feeds.prod.blur.io/socket.io/ -------------------------------------------------------#
    url = "wss://feeds.prod.blur.io/socket.io/"
    ws = websocket.WebSocket()
    ws.connect(url)

    ws.send("40")
    ws.send('4219"subscribe",["0x49cf6f5d44e70224e2e23fdcdd2c053f30ada28b.feeds.activity.eventsCreated"]')

    while True:
        try:
            data = ws.recv()
            event = parseBuffer(data)

            switcher = {
                2: ws.send("3"),
                42: handleFortyTwo(event),
                default: None
            }
            switcher.get(event["code"], lambda: None)()
        except:
            break

    ws.close()

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws, close_status_code, close_msg):
    print("Connection closed")

def on_open(ws):
    print("Connection established")

    # Send the subscription data here
    receive_data()

# Start the websocket connection
websocket.enableTrace(True)
# -------------------------------------------------- v3 url wss://feeds.prod.blur.io/socket.io/?tabId=pKzRU7SRBQDn&storageId=n3kEOR5uxjEF&EIO=4&transport=websocket -------------------------------------------------------#
# ws = websocket.WebSocketApp("wss://feeds.prod.blur.io/socket.io/?tabId=pKzRU7SRBQDn&storageId=n3kEOR5uxjEF&EIO=4&transport=websocket",
#  -------------------------------------------------- v4 url wss://feeds.prod.blur.io/socket.io/ -------------------------------------------------------#
ws = websocket.WebSocketApp('wss://feeds.prod.blur.io/socket.io/',
                            on_message=on_message,
                            on_error=on_error,
                            on_close=on_close)
ws.on_open = on_open
ws.run_forever()